﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;

namespace WP7SampleProject12
{
     public class movieInfoo
        {
         public String MovieName { get; set; }
         public String Link { get; set; }
         public String Time { get; set; }
         public String Place { get; set; }
        }
    

    public partial class movieShowtime : PhoneApplicationPage
    {
        public static String URL;
    
        public movieShowtime()
        {
            InitializeComponent();
            PageTitle.Text = GlobalVariables.movieName;
            progressBar.Visibility = System.Windows.Visibility.Visible;
            progressBar.IsIndeterminate = true;
            DownloadMovieInfo(GlobalVariables.movieName);

        }


        private void DownloadMovieInfo(String MovieName)
        {
            char[] moviename = MovieName.ToCharArray();
            for (int index = 0; index < moviename.Length; index++)
            {
                if (moviename[index] == ' ')
                    moviename[index] = '+';
            }
            MovieName = new string(moviename);
            String loc = Location.city + " " + Location.state;
            char[] location = loc.ToCharArray();
            for (int index = 0; index < location.Length; index++)
            {
                if (location[index] == ' ')
                    location[index] = '+';
            }
            loc = new string(location);
            URL = "http://facemeifudare.appspot.com/movie?name=" + MovieName + "&location=" + loc + "&latitude=" + Location.latitude + "&longitude=" + Location.longitude;
            WebClient twitter = new WebClient();
            twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedURL);
            twitter.DownloadStringAsync(new Uri(URL));
            //txtblock.Text = URL;
        }


        String[] MovieHall;

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            /*
            listBox1.ItemsSource = null;
            
            progressBar.IsIndeterminate = true;
            TextBlock data = (sender as ListBox).SelectedItem as TextBlock;

            //Get the selected ListBoxItem container instance    
            TextBlock selectedItem = listBox1.ItemContainerGenerator.ContainerFromItem(data) as TextBlock;
            txtblock.Text = data.Text;
           */
        }
        private void parser (String data)
        {
            MovieHall = data.Split('\n');
            movieInfoo mi = new movieInfoo();
            List <movieInfoo> m = new List<movieInfoo>();
            try
            {
                listBox1.ItemsSource = from input in MovieHall select new movieInfoo { MovieName = input.Split('%')[0], Link = "  ", Place = input.Split('%')[2], Time = input.Split('%')[3] };
            }
            catch (Exception e)
            {
                MessageBox.Show(URL);
            }
            progressBar.IsIndeterminate = false;
            

        }
        
        void DownloadStringCompletedURL(object sender, DownloadStringCompletedEventArgs e)
        {
            parser(e.Result);
        }

    }

}